const path = require('path')
const htmlWebpackPlugin = require('html-webpack-plugin')
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const CopyPlugin = require("copy-webpack-plugin");

console.log("加载生产模式配置")
module.exports = {
    mode: 'production',
    context: path.resolve(__dirname),
    output: {
        path: path.resolve(__dirname, '../dist'),
        filename: 'js/[name].bundle.js',
        clean: true,
        environment: {
            // 告诉webpack打包后添加的立即执行函数不使用箭头函数
            arrowFunction: false,
            // 不适用const和let
            const: false,
        }
    },
    plugins: [
        new htmlWebpackPlugin({
            filename: 'index.html',
            template: './src/index.html',
            chunks: ['index'],
            minify: {   //优化
                // https://github.com/jantimon/html-webpack-plugin#options
                // collapseWhitespace: true,
                // keepClosingSlash: true,
                // removeComments: true,
                // removeRedundantAttributes: true,
                // removeScriptTypeAttributes: true,
                // removeStyleLinkTypeAttributes: true,
                // useShortDoctype: true
            }
        }),
        new htmlWebpackPlugin({
            filename: 'page.html',
            template: './src/page1.html',
            chunks: ['page1'],
            minify: {   //优化
                // https://github.com/jantimon/html-webpack-plugin#options
                // collapseWhitespace: true,
                // keepClosingSlash: true,
                // removeComments: true,
                // removeRedundantAttributes: true,
                // removeScriptTypeAttributes: true,
                // removeStyleLinkTypeAttributes: true,
                // useShortDoctype: true
            }
        }),
        new MiniCssExtractPlugin({
            // 类似于 webpackOptions.output 中的选项
            // 所有选项都是可选的
            filename: 'style/[name].css',
        }),

        new CopyPlugin({
            patterns: [
                { from: "./src/video", to: "./video" },
            ],
        }),

    ],
    optimization: {//优化
        // 抽离公共代码块
        splitChunks: {
            cacheGroups: { //缓存组
                common: { //公共模块
                    name: 'commonJS',
                    chunks: 'initial', //一开始就抽离
                    minSize: 0, //最小多少字节抽离
                    minChunks: 2 //引用多少次抽离,写0或写1的话，你就是憨憨
                },
                vendor: { //第三方库
                    test: /node_modules/, //查找路径
                    name: 'vendor',
                    priority: 1, //权重，不设置会直接被上面抽离
                    chunks: 'initial', //一开始就抽离
                    minSize: 0, //最小多少字节抽离
                    minChunks: 2 //引用多少次抽离,写0或写1的话，你就是憨憨
                }
            }
        }
    },

}